#!/usr/bin/env python
# don't like coding:utf8
import re
import sys
import tempfile
import os
import subprocess
from random import randint
from time import time
def bblue(s):
    return '\033[1;34m' + str(s) + '\033[0m'

def run_strategy(*args, input=None, output=None):
    """
    input/output是文件名, 一定要传
    """
    cmd = f'./bin/CodeCraft-2021 <{input} >{output} ' + ' '.join(map(lambda a:str(a), args))
    #print(cmd)
    subprocess.getstatusoutput(cmd)

def judge(input, output):
    cmd = '../judge %s %s' % (input, output)
    #print(cmd)
    rc,out = subprocess.getstatusoutput(cmd)
    lines = out.split('\n')
    cost = int(re.search(r'\d+', lines[0]).group())
    migration = int(re.search(r'\d+', lines[1]).group())
    return cost,migration

def run(param):
    """运行strategy和判题程序，返回cost,migration"""
    input1 = '../data2/training-1.txt'
    input2 = '../data2/training-2.txt'
    _,output1 = tempfile.mkstemp()
    _,output2 = tempfile.mkstemp()
    t1 = time()
    run_strategy(*param, input=input1, output=output1)
    t2 = time()
    ta = round(t2-t1,2)
    t1 = time()
    run_strategy(*param, input=input2, output=output2)
    t2 = time()
    tb = round(t2-t1,2)
    cost1,migration1 = judge(input1, output1)
    cost2,migration2 = judge(input2, output2)
    os.remove(output1)
    os.remove(output2)
    return (cost1, migration1, ta), (cost2, migration2, tb)

ra, rb = run([])
print('cost: %d+%d=%s' % (ra[0], rb[0], bblue(ra[0]+rb[0])))
print('time: %d+%d=%s' % (ra[2], rb[2], bblue(ra[2]+rb[2])))
print('migration: %d+%d=%s' % (ra[1], rb[1], bblue(ra[1]+rb[1])))
